package com.terrier.ui.controls
{
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.IWxTextInputSkin;
    import com.terrier.ui.skins.WxSkinManager;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    /**
     * 当文本内容发生改变时调度
     * @eventType flash.events.Event.CHANGE
     */
    [Event(name = "change", type = "flash.events.Event")]

    /**
     * 文本输入框
     * @author Jarvis.w
     */
    public class WxTextInput extends WxLabel
    {
        /**
         * @private
         */
        protected var _hintTxt:TextField;

        /**
         * 构造函数
         */
        public function WxTextInput()
        {
            super();
        }

        /**
         * @private
         */
        override protected function initialize():void
        {
            super.initialize();

            _textDisplay.type = TextFieldType.INPUT;
            _textDisplay.selectable = true;
            _textDisplay.addEventListener(Event.CHANGE, textChangeHandler);

            _hintTxt = new TextField();
            _hintTxt.mouseEnabled = false;

            addChildBelow(_hintTxt, _textDisplay);
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        /**
         * 用户没有输入文本内容时的默认提示文本
         * @return
         */
        public function get hint():String
        {
            return _hintTxt.text;
        }

        /**
         * @private
         */
        public function set hint(value:String):void
        {
            _hintTxt.text = value == null ? "" : value;
        }

        /**
         * 获取或者设置一个布尔值, 指示文本是否可以编辑
         * @return
         */
        public function get editable():Boolean
        {
            return _textDisplay.type == TextFieldType.INPUT && _textDisplay.selectable;
        }

        /**
         * @private
         */
        public function set editable(value:Boolean):void
        {
            _textDisplay.type = value ? TextFieldType.INPUT : TextFieldType.DYNAMIC;
            _textDisplay.selectable = value;
        }

        /**
         * @private
         */
        override public function set text(value:String):void
        {
            super.text = value;
            textChangeHandler(null);
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getTextInputSkin(skinID));
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            super.setSkin(value);

            if (skin != null)
            {
                if (skin.hintStyle != null)
                {
                    skin.hintStyle.applyStyle(_hintTxt);
                }
            }
            invalidateView();
        }

        private function textChangeHandler(e:Event):void
        {
            invalidateView();
            invalidateLayout();
            if (e != null)
            {
                dispatchEvent(e);
            }
        }
		
		private function mouseDownHandler(e:MouseEvent):void
		{
			if (stage != null)
			{
				stage.focus = _textDisplay;
			}
		}

        /**
         * @private
         */
        override protected function updateLayout():void
        {
            super.updateLayout();

            _hintTxt.width = _textDisplay.width;
            _hintTxt.height = _textDisplay.height;

            _hintTxt.x = _textDisplay.x;
            _hintTxt.y = _textDisplay.y;

            if (skin != null && skin.hintStyle != null)
            {
                skin.hintStyle.applyLayout(_hintTxt);
            }
        }

        /**
         * @private
         */
        override protected function updateView():void
        {
            super.updateView();

            var clear:Boolean = _textDisplay.text == "" || _textDisplay.text == null;
            clear ? addChildBelow(_hintTxt, _textDisplay) : removeChildSafely(_hintTxt);
        }

        private function get skin():IWxTextInputSkin
        {
            return _skin as IWxTextInputSkin;
        }
    }
}
